<template>
  <div class="employeeManagement">
    <!-- 页面标题 -->
    <div class="pageHeader">
      <h1>员工管理</h1>
    </div>
    
    <!-- 搜索和筛选区域 -->
    <el-card class="searchCard">
      <el-form :inline="true" :model="searchForm" class="demoFormInline">
        <el-form-item label="用户名">
          <el-input style="width: 200px;" v-model="searchForm.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select style="width: 200px;" v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-left: auto;">
          <el-button type="primary" @click="handleAddEmployee">新增员工</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 数据表格 -->
    <el-card>
      <el-table :data="employeeDataList" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="员工ID" width="80" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="realName" label="真实姓名" width="120" />
        <el-table-column prop="email" label="邮箱" min-width="180" />
        <el-table-column prop="phone" label="手机号" min-width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getEmployeeStatusTagType(scope.row.status)">
              {{ getEmployeeStatusLabel(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEditEmployee(scope.row)">
              编辑
            </el-button>
            <el-button size="small" type="danger" @click="handleDeleteEmployee(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页组件 -->
      <div class="paginationContainer">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[1,10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 新增/编辑员工对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600px"
      @close="handleDialogClose"
    >
      <el-form
        ref="userFormRef"
        :model="employeeForm"
        :rules="formRules"
        label-width="120px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="employeeForm.username"
            placeholder="请输入用户名"
            :disabled="isEditMode"
          />
        </el-form-item>
        
        <el-form-item label="密码" :prop="isEditMode ? '' : 'password'">
          <el-input
            v-model="employeeForm.password"
            type="password"
            placeholder="请输入密码"
            :show-password="true"
          />
          <div v-if="isEditMode" class="formHint">不修改密码请留空</div>
        </el-form-item>
        
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="employeeForm.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="employeeForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="employeeForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="employeeForm.status"
            active-value="1"
            inactive-value="0"
            active-text="正常"
            inactive-text="禁用"
          />
        </el-form-item>
        
        <!-- 企业ID字段（不可编辑） -->
        <el-form-item label="所属企业ID">
          <el-input 
            v-model="employeeForm.enterpriseId" 
            placeholder="企业ID" 
            disabled 
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleFormSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted } from 'vue';
import useEmployeeManagement from '../hooks/useEmployeeManagement';

// 使用员工管理hook
const {
  // 状态
  loading,
  dialogVisible,
  userFormRef,
  // 数据
  searchForm,
  pagination,
  employeeDataList,
  statusOptions,
  employeeForm,
  formRules,
  // 计算属性
  isEditMode,
  dialogTitle,
  // 方法
  fetchEmployeeData,
  handleAddEmployee,
  handleEditEmployee,
  handleFormSubmit,
  handleDeleteEmployee,
  handleDialogClose,
  handleSearch,
  resetSearch,
  handleSizeChange,
  handleCurrentChange,
  getEmployeeStatusTagType,
  getEmployeeStatusLabel
} = useEmployeeManagement();

// 组件挂载时获取数据
onMounted(() => {
  fetchEmployeeData();
});
</script>

<style scoped>
.employeeManagement {
  padding: 20px;
}

.pageHeader {
  margin-bottom: 20px;
}

.pageHeader h1 {
  font-size: 24px;
  font-weight: 500;
  margin: 0;
}

.searchCard {
  margin-bottom: 20px;
}

.paginationContainer {
  margin-top: 20px;
  text-align: right;
}

.formHint {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}
</style>